import jdk.nashorn.internal.scripts.JD;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.postgis.PostgisNGDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.map.FeatureLayer;
import org.geotools.map.MapContent;
import org.geotools.renderer.GTRenderer;
import org.geotools.renderer.lite.StreamingRenderer;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.JMapPane;
import org.geotools.swing.data.JDataStoreWizard;
import org.geotools.swing.wizard.JWizard;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import javax.sql.DataSource;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Frame {
    public static void main(String[] args) throws IOException {
        showFrame2();
    }

    // 使用JFrame渲染地图
    public static void showFrame1() throws  IOException{
        MapContent content = new MapContent();
        FileDataStore dataStore = FileDataStoreFinder.getDataStore(new File("src/main/resources/china_boundary/china_boundary.shp"));
        SimpleFeatureSource featureSource = dataStore.getFeatureSource();

        Style style = SLD.createSimpleStyle(featureSource.getSchema());
        FeatureLayer featureLayer = new FeatureLayer(featureSource, style);

        content.addLayer(featureLayer);
//        JMapFrame.showMap(content);

        JMapPane pane = new JMapPane();
        pane.setRenderer(new StreamingRenderer());
        pane.setMapContent(content);
        // 1. set bounds
        CoordinateReferenceSystem crs = featureSource.getSchema().getCoordinateReferenceSystem();
        ReferencedEnvelope bounds = new ReferencedEnvelope(80, 120, 30, 50, crs);
        pane.setDisplayArea(bounds);
        // 2. add toolbar
        JPanel toolBar = new JPanel();
        JButton btn1 = new JButton("点击按钮");
        addClickEventListener(btn1);
        toolBar.add(btn1);

        JFrame frame = new JFrame("demo");
        frame.setLayout(new BorderLayout());
        frame.add(pane, BorderLayout.CENTER);
        frame.add(toolBar, BorderLayout.NORTH);
        frame.setSize(1800, 1000);
        frame.setVisible(true);
    }

    // 使用JMapFrame渲染地图
    public static void showFrame2() throws IOException {
        MapContent content = new MapContent();
        FileDataStore dataStore = FileDataStoreFinder.getDataStore(new File("src/main/resources/china_boundary/china_boundary.shp"));
        SimpleFeatureSource featureSource = dataStore.getFeatureSource();

        Style style = SLD.createSimpleStyle(featureSource.getSchema());
        FeatureLayer featureLayer = new FeatureLayer(featureSource, style);
        content.addLayer(featureLayer);

        JMapFrame frame = new JMapFrame(content);
        frame.setSize(1200, 800);
        frame.enableStatusBar(true);
        frame.enableToolBar(true);
        frame.enableLayerTable(true);
        frame.setVisible(true);
    }


    public static void addClickEventListener(JButton button){
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("按钮点击");
            }
        });
    };
}
